package com.burgeon.backend.SHOPBACKEND.rest;
/**
 *小分类
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.Dimdef;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetSamallDIMDEF extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetSamallDIMDEF(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<Dimdef> getData(String id) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(id),url);
        System.err.println(str);

        JSONObject jsonObject = new  JSONArray(str).getJSONObject(0);
        String code = jsonObject.getString("code");
        String message = jsonObject.getString("message");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Dimdef> data = new ArrayList<Dimdef>();

        for (int i = 0; i < jsonArray.length(); i++) {
            Dimdef dimdef = new Dimdef();
            dimdef.setId(jsonArray.getJSONArray(i).getString(0));
            dimdef.setDim(jsonArray.getJSONArray(i).getString(1));
            dimdef.setName(jsonArray.getJSONArray(i).getString(2));
            data.add(dimdef);
        }

        return data;
    }

    private MultiValueMap Data(String id) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("DIMFLAG");
        columns.add("ATTRIBNAME");

        params.put("columns", columns);
        params.put("table", "M_DIM");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();
        String ac = "'="+id+"'";
        paramss.put("column", "'M_DIMDEF_ID'");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        GetSamallDIMDEF getRetailByVip = new GetSamallDIMDEF("39.100.45.8:55322", "nea@burgeon.com.cn", "123");
        try {
            System.err.println(getRetailByVip.getData("1"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
